//<>// //<>// //<>// //<>// //<>// //<>// //<>// //<>//

/* incoming osc message are forwarded to the oscEvent method. */

/* these fetch routines could be expanded to return an array with other info
 such as string info like "on" and... ??  */
String addr="";
boolean setWindowAddress=false;
boolean setWindowSize=false;
boolean showBloomAddress=false;
boolean makeToneTrigger=false;

String ipSelected="IP0";

float oscFloatFetch (OscMessage theOscMessage, int which) {
  float theValue=theOscMessage.get(which).floatValue();
  return theValue;
}

void echoAction(int action, int keyIn, float keyFlo) {
  int oscIndex=keyIn+1;
  boolean state=false;
  OscMessage echoControl;
  switch(action) {
  case 0:
    statMsg="clear controls";
    println (statMsg);
    //transition selector
    echoControl = new OscMessage("/MAIN/transition");
    echoControl.add(0.0);
    echoControl.add(1.0);
    oscP5x.send(echoControl, Conductor);
    //flowers
    echoControl = new OscMessage("/MAIN/FlowerState");
    for (int i : flowers) {
      flowers[i]=0;
      echoControl.add(0.0);
    } 
    oscP5x.send(echoControl, Conductor);
    //facets    
    echoControl = new OscMessage("/MAIN/FacetState");
    for (int i : facets) {
      facets[i]=0;
      echoControl.add(0.0);
    }   
    oscP5x.send(echoControl, Conductor);
    break;
  case 1:
    if (flowers[keyIn]==1) {
      state = false;
    } else {
      state = true;
    }
    statMsg = "Flower "+keyIn+ " " +state;
    println (statMsg);
    flowers[keyIn]=int(state);
    echoControl = new OscMessage("/MAIN/FlowerState/1/"+oscIndex);
    echoControl.add(flowers[keyIn]); 
    oscP5x.send(echoControl, Conductor);
    break;
  case 2:
    if (facetName[keyIn]!="") {
      statMsg = "Facet "+facetName[keyIn]; 
      println (statMsg);
      echoControl = new OscMessage("/MAIN/FacetState");
      for (int i=0; i< facets.length; i++) {
        if (i==keyIn) {
          facets[i]=1;
          echoControl.add(1.0);
        } else {
          facets[i]=0;
          echoControl.add(0.0);
        }
      }
      knownFacet=keyIn;
      oscP5x.send(echoControl, Conductor);
    }
    break; 
  case 3:
    statMsg="Launch "+facetName[knownFacet];
    //    println ("statMsg");
    echoControl = new OscMessage("/MAIN/go");
    echoControl.add(1.0);
    oscP5.send(echoControl);
    echoControl = new OscMessage("/MAIN/go");
    echoControl.add(0.0);
    oscP5.send(echoControl);
    break;
  case 4:
    transMode=keyIn;
    echoControl = new OscMessage("/MAIN/transition");
    if (transMode==1) {
      statMsg="Fade enabled";
      echoControl.add(1.0);
      echoControl.add(0.0);
    } else {
      statMsg=("Cut enabled");
      echoControl.add(0.0);
      echoControl.add(1.0);
    }
    println (statMsg);
    println (echoControl);
    oscP5x.send(echoControl, Conductor);
    break;
  case 5:  // rotating things
    switch (keyIn) {
    case 0: // lock toggle
      rotLoc=!rotLoc;
      rotCTRLs();
      if (goFacet==100) {
        echoControl=new OscMessage("/PLAN/degree");
      }
      else {
        echoControl=new OscMessage("/MAIN/degree");
      }
      echoControl.add(oscRot);
      oscP5x.send(echoControl, Conductor);
      echoControl=new OscMessage("/MAIN/rotLoc");
      echoControl.add(int(rotLoc));
      oscP5x.send(echoControl, Conductor);
      break;
    case 1: // limit toggle
      echoControl=new OscMessage("/MAIN/rotLim");
      echoControl.add(int(rotLim));
      oscP5x.send(echoControl, Conductor);
      break;
    case 2: // zero the wheel
      oscRotZero();
      FacetAct=true;
      break;

    case 3: 
    case 4: //change values
      FacetAct=true;
      if (keyIn==4) {
        oscRot=keyFlo;
      } else {
        oscRot+=keyFlo;
      }
      
      if (goFacet==100) {
        echoControl=new OscMessage("/PLAN/degree");
      }
      else {
        echoControl=new OscMessage("/MAIN/degree");
      }
      echoControl.add(oscRot);
      oscP5x.send(echoControl, Conductor);
      echoControl=new OscMessage("/MAIN/spinDirect");
      echoControl.add(oscRot);
      oscP5x.send(echoControl, Conductor);
      break;
    default:
      break;
    }/* end ection 5 code */

  case 6:
    statMsg = "Cherish select "+keyIn; 
    println (statMsg);
    echoControl = new OscMessage("/PLAN/toggle");
    for (int i=0; i< 15; i++) {
      if (i==keyIn) {
        echoControl.add(1.0);
        ipSelected=bloom[2];
      } else {
        echoControl.add(0.0);
      }
    }
    cherishSelect=keyIn;
    oscP5x.send(echoControl, Conductor);    


    break;
  case 7:
    echoControl = new OscMessage("/PLAN/name"+str(keyIn));
    echoControl.add(cherish[1]);
    oscP5x.send(echoControl, Conductor);
    echoControl = new OscMessage("/PLAN/mac"+str(keyIn));
    echoControl.add(bloom[3]);    
    oscP5x.send(echoControl, Conductor);
    echoControl = new OscMessage("/PLAN/ip"+str(keyIn));
    echoControl.add(bloom[2]);    
    oscP5x.send(echoControl, Conductor);
    break;
  default:
    break;
  }
  DisplayAct=true;
}

void oscEvent(OscMessage theOscMessage) {
  OscAct=true;
  addr = theOscMessage.addrPattern();
  inputMsg=addr;

  print("### received an osc message: "+theOscMessage);
  println(" addrpattern: "+addr);
  println(" typetag: "+theOscMessage.typetag());

  if ((addr.indexOf("SHUFFLE")>-1)||(addr.indexOf("DECK")>-1)) {
    deckHandler(theOscMessage, addr);
  }
  if (addr.indexOf("PLAN")>-1) {
    planHandler(theOscMessage, addr);
  }
  /* rotations */
  if (addr.indexOf("MAIN/rot")>-1) {
    int code=0; //  lock toggle code default, no reason, something gets to be
    if (addr.indexOf("Loc")>-1) {
      statMsg="toggle rotLoc";
    }
    if (addr.indexOf("Lim")>-1) {
      // ##  later add the check to the facet for rotation limits, make compare
      statMsg="toggle rotLim";
      code=1;
    }      
    if ((addr.indexOf("rotZero")>-1)&&(!rotLoc)) {
      statMsg="zero rotation";
      code=2;
    }
    sendRot(theOscMessage, code);
  }

  //Transition
  if (addr.indexOf("transition")>-1) {
    transState(theOscMessage);
  }

  //Clear
  if (addr.indexOf("clear")>-1) {
    echoAction(0, 0, 0.0);
  }

  // FlowerState
  if (addr.indexOf("FlowerState/1/")>-1) {
    flowerState(theOscMessage);
  }
  /* detect FacetState msg */
  if (addr.indexOf("FacetState/1/")>-1) {
    facetState(theOscMessage);
  }
  /* init setup */
  if (addr.indexOf("init")>-1) {
    envSetup();
  }

  /*  ## LISTEN TRIGGER controls for facets -- 
   only active flowers will respond  */

  if ((flowers[15]==1)||(flowers[int(bloom[0])]==1)) {
    println("thru flower filter gate"); 
    /* spin */
    if (addr.indexOf("MAIN/spin")>-1) {
      if (!rotLoc) {
        if (addr.indexOf("Fine")>-1) {
          sendSpin(theOscMessage, 3);
        } 
        if (addr.indexOf("Direct")>-1) {        
          sendSpin(theOscMessage, 4);
        }
        if (addr.indexOf("Up")>-1)
        {
          sendSpin(theOscMessage, 72);
        }
        if (addr.indexOf("Dn")>-1)
          sendSpin(theOscMessage, -72);
      }
    }


    /* xy1 */
    if (addr.indexOf("xy1")>-1) {
      println("reading the XY slider");
      sendXY(theOscMessage);
    }
    // rotary1-4, slider1-4
    if (addr.indexOf("fader")>-1) {
      print("fading out...");
      sendFade(theOscMessage);
    }
    //disrupt  
    if (addr.indexOf("color")>-1) {
      sendDisrupt(theOscMessage);
    }
    //GO  
    if (addr.indexOf("go")>-1) {
      println("launch!");
      facetGo();
    }
  }
  DisplayAct=true;
  OscAct=false;
} // end oscEvent
void sendSpin(OscMessage theOscMessage, int code) {
  float ctrlValue=oscFloatFetch(theOscMessage, 0);
  println("rot value: "+ctrlValue);
  /* the following trap may be a problem. trying to stop the button release
   from the up and down 72 spinners from sending two messages, on the press and release
   think about this..*/
  if (ctrlValue!=0) { 
    if ((code!=3)&&(code!=4)) {
      ctrlValue =code;
      code=3;
    }
    echoAction(5, code, ctrlValue);
  }
}

void sendRot(OscMessage theOscMessage, int code) {
  float ctrlValue=oscFloatFetch(theOscMessage, 0);
  echoAction(5, code, ctrlValue);
}
void sendXY(OscMessage theOscMessage) {
  float xValue=oscFloatFetch(theOscMessage, 0);
  float yValue=oscFloatFetch(theOscMessage, 1);
  println ("X: "+xValue+ " Y: " +yValue);
  oscX=xValue*width;
  mouseX=int(xValue*width);
  oscY=yValue*width;
  mouseY=int(xValue*height);
  //  now hook up the data
}

void sendDisrupt(OscMessage theOscMessage) {
  String buffer[] = split(addr, "/");
  int colorValue = int(buffer[3]);    
  println ("Disrupt with hue: "+colorValue);
}

void sendFade(OscMessage theOscMessage) {
  float faderValue=oscFloatFetch(theOscMessage, 0);
  String buffer2[] = split(addr, "/");
  //s
  int thisFader = int(buffer2[3])-1;
  ranges[thisFader]=faderValue;
  println ("Fader "+thisFader+ " set at " +faderValue);
  //  flowers[thisFlower]=int(toggleValue);
}

void transState(OscMessage theOscMessage) {
  String buffer[] = split(addr, "/");
  int thisFlower = (int(buffer[3])-1);
  echoAction(4, thisFlower, 0.0);
}

void flowerState(OscMessage theOscMessage) {
  String buffer[] = split(addr, "/");
  int thisFlower = (int(buffer[4])-1);
  echoAction(1, thisFlower, 0.0);
}

void facetState(OscMessage theOscMessage) {
  String buffer[] = split(addr, "/");
  int thisFacet = (int(buffer[4])-1);
  if (facetName[thisFacet] != "") {
    echoAction(2, thisFacet, 0.0);
  } else {
    knownFacet=16;
  }
}


//end oscTransponder
